package com.kordar.admin.kernel.util;

import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class HtmlUtil {

    public static String layA(String label, String href, Map<String, String> options) {
        StringBuilder str = new StringBuilder(StringUtils.hasText(href) ? "<a lay-href='" + href + "'" : "<a ");
        return getAString(label, options, str);
    }

    private static String getAString(String label, Map<String, String> options, StringBuilder str) {
        for(Map.Entry<String, String> entry : options.entrySet()){
            String key = entry.getKey();
            String value = entry.getValue();
            str.append(" ").append(key).append("=").append("'").append(value).append("'");
        }
        return str.append(">").append(label).append("</a>").toString();
    }

    public static String a(String label, String href, Map<String, String> options) {
        StringBuilder str = new StringBuilder("<a href='" + href + "'");
        return getAString(label, options, str);
    }

    public static String email(String label) {
        return a(label, "mailto:" + label, new HashMap<>());
    }

    public static String tag(String name, String content, Map<String, String> options) {
        StringBuilder str = new StringBuilder("<" + name);
        for(Map.Entry<String, String> entry : options.entrySet()){
            String key = entry.getKey();
            String value = entry.getValue();
            str.append(" ").append(key).append("=").append("'").append(value).append("'");
        }
        return str.append(">").append(content).append("</").append(name).append(">").toString();
    }

    public static String img(String src, Map<String, String> options) {
        StringBuilder str = new StringBuilder("<img src='" + src + "'");
        for (Map.Entry<String, String> entry : options.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            str.append(" ").append(key).append("=").append("'").append(value).append("'");
        }
        return str.append("/>").toString();
    }

    public static String input(String type, Map<String, String> options) {
        StringBuilder str = new StringBuilder("<input type='" + type + "'");
        for (Map.Entry<String, String> entry : options.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            str.append(" ").append(key).append("=").append("'").append(value).append("'");
        }
        return str.append("/>").toString();
    }
}
